package com.zcc.syn;

public class UnsafeBuyTicket {
    public static void main(String[] args) {
        BuyTicket buyTicket = new BuyTicket();

        new Thread(buyTicket,"用户1").start();
        new Thread(buyTicket,"用户2").start();
    }

     static class BuyTicket implements Runnable {
        Boolean flag = true;
        private int ticketNums=10;

        @Override
        public void run() {
            while (flag) {
                try {
                    buy();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        private synchronized void buy() throws InterruptedException {
            if (ticketNums<=0) {
                flag = false;
                return;
            }
            Thread.sleep(100);
            System.out.println(Thread.currentThread().getName()+"拿到了第"+ticketNums--+"张票");
        }
    }


}
